﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewAllSubCommittee : System.Web.UI.Page
    {
        MemberAL memberAL;
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 5;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
            
        }


        private void BindData()
        {

            memberAL = new MemberAL();

            string committeeId = Request.QueryString.ToString();
            List<SubCommitteeDTO> allSubCommitteesOfCommittee = new List<SubCommitteeDTO>();
            List<SubCommitteeDTO> allSubCommitteesOfCommitteeOfOnePage = new List<SubCommitteeDTO>();

            if (committeeId != "")
            {

                allSubCommitteesOfCommittee = memberAL.GetAllSubCommitteeByCommitteeId(new Guid(committeeId)).OrderBy(a=>a.SubCommitteeName).ToList();
               
            }
            else
            {

                allSubCommitteesOfCommittee = memberAL.GetAllSubCommittees().OrderBy(a=>a.SubCommitteeName).ToList();
               
            }
            //List<SubCommitteeDTO> allExCos = memberAL.GetAllCommittees().OrderByDescending(a => a.StartDate).ToList();
            //List<SubCommitteeDTO> allExCosOfOnePage = new List<SubCommitteeDTO>();
            //gvViewAllMembers.DataSource = allMembers;
            //gvViewAllMembers.DataBind();


            // get the total rows 
            double totalRows = (int)allSubCommitteesOfCommittee.Count;

            lblAllSubCommPage.Text = "Displaying all Sub Committees Of Executive Committee - Page ";
            lblAllSubCommCurrPage.Text = currentPageNumber.ToString();
            lblAllSubCommOf.Text = " of ";
            lblAllSubCommTotalPage.Text = CalculateTotalPages(totalRows).ToString();



            int totalPageNumber = lblAllSubCommTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllSubCommTotalPage.Text);

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllSubCommFirst.Enabled = false;
                    btnAllSubCommPrev.Enabled = false;
                    btnAllSubCommNext.Enabled = false;
                    btnAllSubCommLast.Enabled = false;
                    allSubCommitteesOfCommitteeOfOnePage = allSubCommitteesOfCommittee;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllSubCommFirst.Enabled = false;
                    btnAllSubCommPrev.Enabled = false;
                    btnAllSubCommNext.Enabled = true;
                    btnAllSubCommLast.Enabled = true;
                    allSubCommitteesOfCommitteeOfOnePage = allSubCommitteesOfCommittee.GetRange(0, PAGE_SIZE);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllSubCommFirst.Enabled = true;
                    btnAllSubCommPrev.Enabled = true;
                    btnAllSubCommNext.Enabled = true;
                    btnAllSubCommLast.Enabled = true;
                    allSubCommitteesOfCommitteeOfOnePage = allSubCommitteesOfCommittee.GetRange(((currentPageNumber - 1) * PAGE_SIZE), PAGE_SIZE);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllSubCommFirst.Enabled = true;
                    btnAllSubCommPrev.Enabled = true;
                    btnAllSubCommNext.Enabled = false;
                    btnAllSubCommLast.Enabled = false;
                    allSubCommitteesOfCommitteeOfOnePage = allSubCommitteesOfCommittee.GetRange(((currentPageNumber - 1) * PAGE_SIZE), (allSubCommitteesOfCommittee.Count - (currentPageNumber - 1) * PAGE_SIZE));
                }
            }



            gvViewAllSubCommittee.DataSource = allSubCommitteesOfCommitteeOfOnePage;
            gvViewAllSubCommittee.DataBind();


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE);

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllSubCommCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllSubCommCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllSubCommTotalPage.Text);
                    break;
            }


            BindData();
        }

        protected void gvViewAllSubCommittee_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvViewAllSubCommittee.PageIndex = e.NewPageIndex;
            gvViewAllSubCommittee.DataBind();
        }
    }
}